﻿using MiniBottles.Models;
using Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace MiniBottles.Controllers
{
 
    public class CollectionController : Controller
    {
        private DataContext db = new DataContext();
        private int pageSize = 25;

        public ActionResult News(int? page)
        {
            IQueryable<Bottle> result = db.Bottles.Where(x => !x.DictCategory.IsDeleted && !x.IsDeleted && x.IsActive && !x.IsWanted);
            result = result.OrderByDescending(x => x.CreateDate);
            ViewBag.Message = AppResource.News;

            ViewBag.BottleList = result.ToPagedList<Bottle>(page ?? 1, pageSize);
             
            

            return View("Index");
        }
        public ActionResult Wanted(int? page)
        {
            IQueryable<Bottle> result = db.Bottles.Where(x => !x.DictCategory.IsDeleted && !x.IsDeleted && x.IsActive && x.IsWanted);
            ViewBag.Message = AppResource.Wanted;
            result = result.OrderByDescending(x => x.Order);
            ViewBag.BottleList = result.ToPagedList<Bottle>(page ?? 1, pageSize);
            return View("Index");
        }

        public ActionResult Favorite(int? page)
        {
            IQueryable<Bottle> result = db.Bottles.Where(x => !x.DictCategory.IsDeleted && !x.IsDeleted && x.IsActive && !x.IsWanted);
            result = result.Where(x => x.IsFavorite).OrderByDescending(x => x.Order);
            ViewBag.Message = AppResource.Favorite ;

            ViewBag.BottleList = result.ToPagedList<Bottle>(page ?? 1, pageSize);

            return View("Index", result);
        }

        public ActionResult Exchnange(int? page)
        {
            IQueryable<Bottle> result = db.Bottles.Where(x => !x.DictCategory.IsDeleted && !x.IsDeleted && x.IsActive && !x.IsWanted);
            result = result.Where(x => x.ToExchange).OrderByDescending(x=>x.Order);
            ViewBag.Message = AppResource.Exchange;

            ViewBag.BottleList = result.ToPagedList<Bottle>(page ?? 1, pageSize);

            return View("Index", result);
        }

        //
        // GET: /Collection/

        public ActionResult Index(int? page,string query, int categoryId = 0, int collectionId = 0)
        {

            ViewBag.Query = query;
            ViewBag.CategoryId = categoryId;
            ViewBag.CollectionId = collectionId;


            IQueryable<Bottle> result = db.Bottles.Where(x => !x.DictCategory.IsDeleted && !x.IsDeleted && x.IsActive && !x.IsWanted);
            string message = string.Empty;
            if (categoryId > 0)
            {
                result = result.Where(x => x.DictCategory.Id == categoryId);
                var cat = db.DictCategory.Find(categoryId);
                if (Helper.IsPL(Session["Culture"]))
                {
                    message = string.Format("Miniaturki w kategorii : {0}", cat.Name);
                }
                else
                {
                    message = string.Format("Miniatures in category : {0}", cat.Name);
                }
            }
            else if (collectionId > 0)
            {
                result = result.Where(x => x.DictCategory.DictCollection.Id == collectionId);
                var cat = db.DictCollection.Find(collectionId);
                if (Helper.IsPL(Session["Culture"]))
                {
                    message = string.Format("Miniaturki w kolekcji : {0}", cat.Name);
                }
                else
                {
                    message = string.Format("Miniatures in collection : {0}", cat.Name);
                }

            }
            else if (!string.IsNullOrEmpty(query))
            {
                result = result.Where(x => x.Name.Contains(query) || x.Description.Contains(query) || x.DictCategory.Name.Contains(query) || x.DictCategory.DictCollection.Name.Contains(query));

                if (Helper.IsPL(Session["Culture"]))
                {
                    message = string.Format("Wyniki wyszukiwania dla : {0}", query);
                }
                else
                {
                    message = string.Format("Search results for : {0}", query);
                }
            }
            else
            {
                result = result.OrderByDescending(x => x.CreateDate);
                message = AppResource.LastAdded;
            }
            result = result.OrderByDescending(x => x.Order);

            ViewBag.BottleList = result.ToPagedList<Bottle>(page ?? 1, pageSize);
            ViewBag.Message = message;


            return View();
        }

       
        public ActionResult MyCollection()
        {
           var result = db.DictCollection.Where(x => !x.IsDeleted && x.IsActive).ToList();
            return View(result);
        }
       
    }
}
